﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_FacComeAndGoReg_Newest : DockContent
    {
        private bool m_bSumStart = false;
        private Infragistics.Win.UltraWinGrid.UltraGridRow[] m_ugr = null;
        private DGBY.BLL.FinRecvMoneyAuxiliaryTab_New m_bllFinRecvMoneyAuxiliaryTab_New = new DGBY.BLL.FinRecvMoneyAuxiliaryTab_New();
        //private DataTable m_dt = null;
        private const string m_strStartTimeCondition = "'";
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        //private DGBY.BLL.FinRecvMoneyAuxiliaryTab_New m_bllFinRecvMoneyAuxiliaryTab_New = new DGBY.BLL.FinRecvMoneyAuxiliaryTab_New();

        private int m_nBackTaxNotDone = 0;

        public Frm_FacComeAndGoReg_Newest()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂简称";
                this.comboBoxFac.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxFac.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxFac.SelectedIndex = 0;
            }
        }
        private Decimal GetRecvMoneyTotal(string strWhere)
        {
            DataSet ds = m_bllFinRecvMoneyAuxiliaryTab_New.GetRecvMoneyTotalJinE(strWhere);
            Decimal dtTemp = 0;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TotalJinE"].ToString() != "")
                    dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[0]["TotalJinE"].ToString());
            }
            return dtTemp;
        }
        private void Frm_FacComeAndGoReg_Newest_Load(object sender, EventArgs e)
        {
            InitCombox();
            InitUltraWinGridSetting();
        }
        private void ColorColHeader()
        {
            
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["币制"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["开票汇率"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["折前出口总金额"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["折后出口总金额"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["差额"].Header.Appearance.ForeColor = Color.Blue;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["折扣部份利润"].Header.Appearance.ForeColor = Color.Blue;


            this.ultraGrid1.DisplayLayout.Bands[0].Columns["收款方式"].Header.Appearance.ForeColor = Color.Brown;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["结汇日期"].Header.Appearance.ForeColor = Color.Brown;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["折算为USD或HKD"].Header.Appearance.ForeColor = Color.Brown;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["对人民币汇率"].Header.Appearance.ForeColor = Color.Brown;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["付本日期"].Header.Appearance.ForeColor = Color.Brown;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["已付货款"].Header.Appearance.ForeColor = Color.Brown;



            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["付税日期"].Header.Appearance.ForeColor = Color.Salmon;
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["已付税汇率"].Header.Appearance.ForeColor = Color.Salmon;
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["已付税"].Header.Appearance.ForeColor = Color.Salmon;
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["系数A"].Header.Appearance.ForeColor = Color.Salmon;


            this.ultraGrid1.DisplayLayout.Bands[0].Columns["合同金额"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["本金"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["应退税额"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["本金差额"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["退税差额"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["合计"].Header.Appearance.ForeColor = Color.DarkGoldenrod;
            

        }
        private void ShowAllRecordInDataGridView()
        {
            try
            {
                m_nBackTaxNotDone = 0;
                DataSet dsRemainRecvMoney
                    = m_bllFinRecvMoneyAuxiliaryTab_New.GetFinRecvMoneyAuxiliaryInfoForComeAndGo(" FactoryDataTab.FactoryDataTabID = '"
                       + this.comboBoxFac.SelectedValue 
                       + "' and FinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 0 and FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0");

            
                
                int nCount = dsRemainRecvMoney.Tables[0].Rows.Count;

                DataSet dsFacComeAndGo
                       = m_bllBGCKHTTab.GetFacComeAndGoInfoNew(" FactoryDataTab.FactoryDataTabID = '"
                       + this.comboBoxFac.SelectedValue + m_strStartTimeCondition
                       + " and PaidanRegisterTab.PaidanRegisterTabID in (select HxdNoTab.PaidanRegisterTabID from HxdNoTab) ", (int)this.comboBoxFac.SelectedValue);

       
                
                ultraGrid1.DataSource = null;
                ultraGrid1.DataSource = dsFacComeAndGo;
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["审票编号升序"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["PaidanRegisterTabID1"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["付税编号"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["计算公式"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["审票编号"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["美元汇率"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["港币汇率"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["FactoryDataTabid"].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns["排序列"].Hidden = true;
               
                //ultraGrid1.DisplayLayout.Bands[0].Columns["子序号"].Hidden = true;
                

                DataTable dt = dsFacComeAndGo.Tables[0];
                bool bBackTaxDone = true;

                Decimal dtChuKouTotalOther = 0;
                Decimal dtChuKouTotalAndBackTaxDone = 0;
                List<int> lstPaidanID = new List<int>();
                List<int> lstSubRowNo = new List<int>();
                List<int> lstMainRowNo = new List<int>();
                Dictionary<int, decimal> dicPaidanID2PayTotal = new Dictionary<int, decimal>();
                Dictionary<int, decimal> dicPaidanID2PayTax = new Dictionary<int, decimal>(); 
                //寻找已退税分割点
                m_nBackTaxNotDone = dt.Rows.Count;
                int nInitCount = dt.Rows.Count;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Decimal dtChuKou = (Decimal)dt.Rows[i]["折前出口总金额"];
                    
                    dtChuKouTotalOther += dtChuKou;
                    string strPayTaxNo = dt.Rows[i]["审票编号"].ToString();
                    if (bBackTaxDone == true && strPayTaxNo == "")
                    {
            
                        bBackTaxDone = false;
                        m_nBackTaxNotDone = i;
                    }
                    if (bBackTaxDone)
                    {
                        dtChuKouTotalAndBackTaxDone += dtChuKou;
                    }
                }

                if (m_nBackTaxNotDone <= dt.Rows.Count - 1)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nBackTaxNotDone];
                    CurrentUGR.Appearance.BackColor = Color.Yellow;
                }

                int nContractRowsCountRemain = (dt.Rows.Count - 1 - m_nBackTaxNotDone) > 0 ? (dt.Rows.Count - 1 - m_nBackTaxNotDone) : 0;

                if (nCount <= nContractRowsCountRemain && nCount > 0)
                {
                    int nTempCount = nCount;
                    DataTable dtTemp = dsRemainRecvMoney.Tables[0];
                    int nTemp = m_nBackTaxNotDone;
                    for (int i = nContractRowsCountRemain; i < dt.Rows.Count; i++)
                    {
                        nCount--;
                        if (nCount < 0)
                            break;
                        UltraGridRow CurrentUGR = ultraGrid1.Rows[nTemp];
                        CurrentUGR.Cells["收款方式"].Value = dtTemp.Rows[i - nContractRowsCountRemain]["收款方式"].ToString();
                        CurrentUGR.Cells["结汇日期"].Value = (DateTime)dtTemp.Rows[i - nContractRowsCountRemain]["结汇日期"];
                        CurrentUGR.Cells["对人民币汇率"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["对人民币汇率"];
                        CurrentUGR.Cells["折算为USD或HKD"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["折算为USD或HKD"];
                        CurrentUGR.Cells["付本日期"].Value = (DateTime)dtTemp.Rows[i - nContractRowsCountRemain]["付本日期"];
                        CurrentUGR.Cells["已付货款"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["已付货款"];
                        nTemp++;
                    }
                }
                else if (nCount > nContractRowsCountRemain && nCount > 0)
                {
                    DataTable dtTempFacComeAndGo = dsFacComeAndGo.Tables[0];
                    //int nNeedAddRowsCount = dt.Rows.Count - m_nBackTaxNotDone - nContractRowsCountRemain;
                    int nNeedAddRowsCount = nCount;
                    for (int i = 0; i < nNeedAddRowsCount; i++)
                    {
                        DataRow dr = dtTempFacComeAndGo.NewRow();
                        dtTempFacComeAndGo.Rows.Add(dr);
                    }
                    DataTable dtTemp = dsRemainRecvMoney.Tables[0];
                    int nTemp = m_nBackTaxNotDone;
                    for (int i = nContractRowsCountRemain; i < dt.Rows.Count; i++)
                    {
                        nCount--;
                        if (nCount < 0)
                            break;
                        UltraGridRow CurrentUGR = ultraGrid1.Rows[nTemp];
                        CurrentUGR.Cells["收款方式"].Value = dtTemp.Rows[i - nContractRowsCountRemain]["收款方式"].ToString();
                        CurrentUGR.Cells["结汇日期"].Value = (DateTime)dtTemp.Rows[i - nContractRowsCountRemain]["结汇日期"];
                        CurrentUGR.Cells["对人民币汇率"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["对人民币汇率"];
                        CurrentUGR.Cells["折算为USD或HKD"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["折算为USD或HKD"];
                        CurrentUGR.Cells["付本日期"].Value = (DateTime)dtTemp.Rows[i - nContractRowsCountRemain]["付本日期"];
                        CurrentUGR.Cells["已付货款"].Value = (Decimal)dtTemp.Rows[i - nContractRowsCountRemain]["已付货款"];
                        nTemp++;
                    }
                }

                //if (m_nBackTaxNotDone > nInitCount - 1 && m_nBackTaxNotDone < nInitCount)
                if (m_nBackTaxNotDone > nInitCount - 1)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nBackTaxNotDone];
                    CurrentUGR.Appearance.BackColor = Color.Yellow;
                }

                //decimal dtTotalPay = 0;
                //decimal dtTotalPayTax = 0;
                for (int i = 0; i < m_nBackTaxNotDone; i++)
                {
                   
                    int nPaidanID = (int)dt.Rows[i]["PaidanRegisterTabID1"];

                    if (lstPaidanID.Contains(nPaidanID))
                    {
                        lstSubRowNo.Add(i);
                       
                        dicPaidanID2PayTotal[nPaidanID] += (decimal)dt.Rows[i]["已付货款"];
                        dicPaidanID2PayTax[nPaidanID] += (decimal)dt.Rows[i]["已付税"];
                    }

                    if (!lstPaidanID.Contains(nPaidanID))
                    {
                        lstPaidanID.Add(nPaidanID);
                        lstMainRowNo.Add(i);
                        dicPaidanID2PayTotal.Add(nPaidanID, (decimal)dt.Rows[i]["已付货款"]);
                        dicPaidanID2PayTax.Add(nPaidanID, (decimal)dt.Rows[i]["已付税"]);
                    }

                  
                }

                foreach (int nRowIndex in lstSubRowNo)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nRowIndex];
              
                    CurrentUGR.Cells["申报日期"].Value = DBNull.Value;
                    CurrentUGR.Cells["币制"].Value = DBNull.Value;
                    CurrentUGR.Cells["集装箱柜号"].Value = DBNull.Value;
                    CurrentUGR.Cells["开票汇率"].Value = DBNull.Value;
                    CurrentUGR.Cells["折前出口总金额"].Value = DBNull.Value;
                    CurrentUGR.Cells["折后出口总金额"].Value = DBNull.Value;
                    CurrentUGR.Cells["已审票金额"].Value = DBNull.Value;

                    CurrentUGR.Cells["差额"].Value = DBNull.Value;
                    CurrentUGR.Cells["折扣部份利润"].Value = DBNull.Value;


                    CurrentUGR.Cells["合同金额"].Value = DBNull.Value;
                    CurrentUGR.Cells["本金"].Value = DBNull.Value;
                    CurrentUGR.Cells["应退税额"].Value = DBNull.Value;
                    CurrentUGR.Cells["本金差额"].Value = DBNull.Value;
                    CurrentUGR.Cells["退税差额"].Value = DBNull.Value;
                    CurrentUGR.Cells["合计"].Value = DBNull.Value;
                    
                }
                foreach (int nRowIndex in lstMainRowNo)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nRowIndex];
                    int nPaidanID = (int)CurrentUGR.Cells["PaidanRegisterTabID1"].Value;
                    decimal dtBenJin = (decimal)CurrentUGR.Cells["本金"].Value;
                    decimal dtPayTax = (decimal)CurrentUGR.Cells["应退税额"].Value;

                    CurrentUGR.Cells["本金差额"].Value = dtBenJin - dicPaidanID2PayTotal[nPaidanID];
                    CurrentUGR.Cells["退税差额"].Value = dtPayTax - dicPaidanID2PayTax[nPaidanID];
                    CurrentUGR.Cells["合计"].Value = (dtBenJin - dicPaidanID2PayTotal[nPaidanID]) + (dtPayTax - dicPaidanID2PayTax[nPaidanID]);
                

                }

                Decimal dtRecv = GetRecvMoneyTotal("FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0 and FinRecvMoneyDetailTab_New.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue.ToString() + "'");

                Decimal dtRecvBackTax = GetRecvMoneyTotal("FinRecvMoneyDetailTab_New.isNotJoinBackTax = 0 and FinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 1 and FinRecvMoneyDetailTab_New.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue.ToString() + "'");


                DataSet dsChuKou = m_bllBGCKHTTab.GetFacChuKouTotal(" FactoryDataTab.FactoryDataTabID = '"
                       + this.comboBoxFac.SelectedValue + m_strStartTimeCondition
                       + " and PaidanRegisterTab.PaidanRegisterTabID in (select HxdNoTab.PaidanRegisterTabID from HxdNoTab) ");


                Decimal dtChuKouTotal = (Decimal)dsChuKou.Tables[0].Rows[0]["报关金额"];
                this.textBoxChouKouBackTaxDoneTotal.Text = (dtChuKouTotalAndBackTaxDone - (dtChuKouTotalOther - dtChuKouTotal)).ToString("0.00");
                this.textBoxChuKouAll.Text = dtChuKouTotal.ToString("0.00");
                this.textBoxRecvTotal.Text = dtRecv.ToString("0.00");
                this.textBoxTotalDiff.Text = (dtChuKouTotal - dtRecv).ToString("0.00");

                this.textBoxNotBackTaxChuKou.Text = (dtChuKouTotal - (dtChuKouTotalAndBackTaxDone - (dtChuKouTotalOther - dtChuKouTotal))).ToString("0.00");
                //this.textBoxNotBackTaxRecvMoney.Text = (dtRecv - (dtChuKouTotalAndBackTaxDone - (dtChuKouTotalOther - dtChuKouTotal))).ToString("0.00");
                this.textBoxNotBackTaxRecvMoney.Text = (dtRecv - dtRecvBackTax).ToString();
                this.textBoxNotBackTaxDiff.Text = (dtChuKouTotal - (dtChuKouTotalAndBackTaxDone - (dtChuKouTotalOther - dtChuKouTotal)) - (dtRecv - dtRecvBackTax)).ToString("0.00");
               
            }
            catch (Exception ex)
            {
            }
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ColorColHeader();
            //this.ultraGrid1.DisplayLayout.UseFixedHeaders = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].Header.Fixed = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Header.Fixed = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["币制"].Header.Fixed = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["出口总金额"].Header.Fixed = true;

            //ultraGrid1.DisplayLayout.Override.FixedHeaderIndicator = FixedHeaderIndicator.None;  // 不显示小图标

            
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.LocalPrintPreveiw();

            ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;
            InitColumnWidth();
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["币制"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["集装箱柜号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票汇率"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折前出口总金额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折后出口总金额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["差额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折扣部份利润"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款方式"].Width = 70;
            ultraGrid1.DisplayLayout.Bands[0].Columns["结汇日期"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折算为USD或HKD"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对人民币汇率"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付本日期"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["已付货款"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同金额"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["本金"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应退税额"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["本金差额"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["退税差额"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合计"].Width = 75;
        }

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataSet ds = (DataSet)ultraGrid1.DataSource;
            m_ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            DataTable dt = ds.Tables[0];

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            int iRdex = e.CurrentRowIndex;
            int iCdex = e.CurrentColumnIndex;
            //e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].CellFormat.FillPatternForegroundColor = Color.FromArgb(0, 0, 0); 
            // string strColumnName = e.GridColumn;
            if (e.GridColumn.Key == "已付税")
            {
                //m_ugr[iRdex-1].Cells[e.GridColumn.Key + "公式"].Value.ToString()
                e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].ApplyFormula(m_ugr[iRdex - 1].Cells["计算公式"].Value.ToString() == "" ? "=0" : "=" + m_ugr[iRdex - 1].Cells["计算公式"].Value.ToString());
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;

            ultraGrid1.DisplayLayout.Bands[0].Columns["美元汇率"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["港币汇率"].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns["FactoryDataTabid"].Hidden = true;
            string strID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["FactoryDataTabid"].Text;
            string strBizhi = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["币制"].Text;
            int nID = Convert.ToInt32(strID, 10);
            if (nID == 26 && strBizhi == "港币")//以纯
            {
                string strUsdHuilv = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["美元汇率"].Text;
                string strHkdHuilv = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["港币汇率"].Text;
                string strJinE = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["折前出口总金额"].Text;
                string strDicountJinE = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["折后出口总金额"].Text;

                Decimal dtUsdHuilv = Convert.ToDecimal(strUsdHuilv);
                Decimal dtHkdHuilv = Convert.ToDecimal(strHkdHuilv) == 0 ? 1 : Convert.ToDecimal(strHkdHuilv);
                Decimal dtJinE = Convert.ToDecimal(strJinE);
                Decimal dtDicountJinE = Convert.ToDecimal(strDicountJinE);

                this.textBoxYiChunDisJinE.Text = (dtDicountJinE * dtUsdHuilv / dtHkdHuilv).ToString("0.00");
                this.textBoxYichunJinE.Text = (dtJinE * dtUsdHuilv / dtHkdHuilv).ToString("0.00");
            }
            if (m_bSumStart)
            {
                if (this.ultraGrid1.ActiveCell.Appearance.BackColor.Name == "0")
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.Aqua;
                }
                else
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.FromName("0");
                }
            }
        }

        private void buttonSumStart_Click(object sender, EventArgs e)
        {
            m_bSumStart = true;
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            for (int i = 0; i < ugr.Length; i++)
            {
                for (int j = 0; j < ugr[i].Cells.Count; j++)
                {
                    if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                    {
                        ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                    }
                }
            }
        }

        private void buttonSumStop_Click(object sender, EventArgs e)
        {
            try
            {
                Decimal dtTotal = 0;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    for (int j = 0; j < ugr[i].Cells.Count; j++)
                    {
                        if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                        {
                            Decimal dtTemp = Convert.ToDecimal(ugr[i].Cells[j].Value.ToString());
                            dtTotal += dtTemp;
                            //ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                        }
                    }
                }

                this.textBoxTotal.Text = dtTotal.ToString();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
    }
}
